/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts.personal;

import hibernate.Clientes;
import hibernate.Cuentas;
import hibernate.CuentasDAO;
import hibernate.CuentasHasClienteDAO;
import hibernate.Sucursales;
import hibernate.SucursalesDAO;
import java.util.Formatter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import util.DigitoControl;

/**
 *
 * @author Winnie
 */
public class AltaCuentaAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FORMULARIO = "formulario";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (request.getParameter("alta") != null) {
            AltaCuentaForm f = (AltaCuentaForm) form;
            CuentasDAO DAOCuenta = new CuentasDAO();
            
            // cogemos de sesion el cliente
            HttpSession session = request.getSession();
            Clientes cliente = (Clientes)session.getAttribute("cliente");            
            
            // creamos la cuenta
            Cuentas cuenta = new Cuentas(2100, Integer.valueOf(f.getPkSucursal()), 10, 0, f.getTipo());       
            Integer numCuenta = DAOCuenta.create(cuenta);
            
            Formatter fmt1 = new Formatter();
            fmt1.format("%04d",Integer.valueOf(f.getPkSucursal()));
            Formatter fmt2 = new Formatter();
            fmt2.format("%010d",numCuenta);
            
            DigitoControl calculaDC = new DigitoControl();
            calculaDC.setSucursal(fmt1.toString());
            String DC= calculaDC.calculo_dc(fmt2.toString());
            
            // actualizamos el DC en la cuenta
            DAOCuenta.updateDC(numCuenta, DC);
            
            // guardamos su relacion de cuenta y cliente
            CuentasHasClienteDAO dao = new CuentasHasClienteDAO();
            dao.create(cliente, cuenta, "titular");
            
            return mapping.findForward(SUCCESS);

        }
        SucursalesDAO DAOSucursal = new SucursalesDAO();
        List<Sucursales> listaSucursales = DAOSucursal.getList();
        request.setAttribute("listaSucursales", listaSucursales);
        return mapping.findForward(FORMULARIO);

    }
}
